package com.cljz.makepassenger.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.model.net.bean.InviteListBean;
import com.cljz.makepassenger.ui.holder.SendOrReceiveInviteHolder;

import java.util.List;

/**
 * Created by apple on 2017/4/12.
 */

public class SendOrReceiveInviteAdapter extends RecyclerView.Adapter<SendOrReceiveInviteHolder> {
    private Context context;
    private List<InviteListBean.DataBean> inviteList;
    private OnItemClickListener mOnItemClickListener;


    public SendOrReceiveInviteAdapter(Context context, List<InviteListBean.DataBean> inviteList) {
        this.context = context;
        this.inviteList = inviteList;
    }

    @Override
    public SendOrReceiveInviteHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SendOrReceiveInviteHolder(View.inflate(context, R.layout.item_inviter_list, null));
    }

    @Override
    public void onBindViewHolder(SendOrReceiveInviteHolder holder, final int position) {
        holder.setData(inviteList, position);
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onClick(inviteList.get(position));
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.onLongClick(inviteList.get(position));
                    return false;
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return inviteList == null ? 0 : inviteList.size();
    }

    public void setData(List<InviteListBean.DataBean> data) {
        this.inviteList = data;
        notifyDataSetChanged();
    }

    public void addData(List<InviteListBean.DataBean> data) {
        int startPosition = this.inviteList.size() + 1;
        this.inviteList.addAll(data);
        notifyItemRangeInserted(startPosition,data.size());
    }

    public interface OnItemClickListener {
        void onClick(InviteListBean.DataBean dataBean);

        void onLongClick(InviteListBean.DataBean dataBean);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
}
